package com.zhu.aliyun.controller;

import com.alibaba.fastjson.JSONObject;
import com.zhu.aliyun.service.FileUploadService;
import com.zhu.commons.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RequestMapping("admin/aliyun/file")
@Api(tags = "阿里云文件上传")
@RestController
//@CrossOrigin
@Slf4j
public class UploadController {
   @GetMapping("test")
   @ApiOperation("测试")
    public String hello(){
       return "hello";
   }

    @Autowired
    private FileUploadService fileUploadService;

    /*
     * 文件上传api
     * @param: file
     * @return: com.alibaba.fastjson.JSONObject
     * @create: 2020/10/31 17:35
     * @author: csp1999
     */
    @ApiOperation(value = "文件上传")
    @PostMapping("upload")
    public R upload(@RequestParam("file") MultipartFile file) {
        JSONObject jsonObject = new JSONObject();
        if (file != null) {
            String returnFileUrl = fileUploadService.upload(file);
            if (returnFileUrl.equals("error")) {
                jsonObject.put("error", "文件上传失败！");
                return R.error().message("文件上传失败");
            }
            jsonObject.put("success", "文件上传成功！");
            jsonObject.put("returnFileUrl", returnFileUrl);
            return R.ok().data("url",returnFileUrl);
        } else {
            jsonObject.put("error", "文件上传失败！");
            return R.error().message("文件上传失败");
        }
    }

    /*
     * 文件下载api
     * @param: fileName
     * @param: response
     * @return: com.alibaba.fastjson.JSONObject
     * @create: 2020/10/31 17:35
     * @author: csp1999
     */
    @ApiOperation(value = "文件下载")
    @GetMapping(value = "download/{fileName}")
    public R download(@PathVariable("fileName") String fileName, HttpServletResponse response) throws Exception {
        JSONObject jsonObject = new JSONObject();

        String status = fileUploadService.download(fileName, response);
        if (status.equals("error")) {
            jsonObject.put("error", "文件下载失败！");
            return R.error().message("文件下载失败");
        } else {
            jsonObject.put("success", "文件下载成功！");
            return R.ok().message("文件下载成功");
        }
    }

    /*
     * 文件删除api
     * @param: fileName
     * @return: com.alibaba.fastjson.JSONObject
     * @create: 2020/10/31 17:35
     * @author: csp1999
     */
    @ApiOperation(value = "文件删除")
    @DeleteMapping("/delete")
    public R DeleteFile(@RequestParam String fileName) {
        //        http://education01bucket.oss-cn-hangzhou.aliyuncs.com/education/2022/08/04/5cf54c94-7d7e-4d5a-9e64-19722920fc1a.jpg
        //截取正确的图片名称
        String newFile = StringUtils.substringAfterLast( fileName,".aliyuncs.com/");

        String status = fileUploadService.delete(newFile);
        if (status.equals("error")) {
            return R.error().message("文件删除失败");
        } else {

         return R.ok().message("文件删除成功！");
        }
    }
    @ApiOperation("文件删除")
    @DeleteMapping("remove/{fileName}")
    public R removeFile(@ApiParam(value = "要删除的文件路径",required = true) @PathVariable String fileName){
        fileUploadService.removeFile(fileName);
        return R.ok().message("文件删除成功");
    }

}


